#!/usr/bin/env python
# -*- coding:utf-8 -*-
from cachelib import file
from flask_restful import Resource, reqparse
from flask import jsonify,request
from service.passageInfoService import PassageInfoService
from utils import commons
from utils.loggings import loggings
from utils.response_code import RET
from werkzeug.datastructures import FileStorage
from controller.passageInfoController import PassageInfoController

class PassageInfoOtherResource(Resource):

    # join table query
    @classmethod
    def joint_query(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('PostAuthorID', type=str, location='args', required=False, help='PostAuthorID参数类型不正确或缺失')
        parser.add_argument('PostContent', type=str, location='args', required=False, help='PostContent参数类型不正确或缺失')
        parser.add_argument('PostTitle', type=str, location='args', required=False, help='PostTitle参数类型不正确或缺失')
        parser.add_argument('PostStatus', type=int, location='args', required=False, help='PostStatus参数类型不正确或缺失')
        parser.add_argument('UpVoteNum', type=int, location='args', required=False, help='UpVoteNum参数类型不正确或缺失')
        parser.add_argument('CommentNum', type=int, location='args', required=False, help='CommentNum参数类型不正确或缺失')
        parser.add_argument('LookNUM', type=int, location='args', required=False, help='LookNUM参数类型不正确或缺失')
        parser.add_argument('IsDelete', type=int, location='args', required=False, help='IsDelete参数类型不正确或缺失')
        parser.add_argument('CreateTime', type=str, location='args', required=False, help='CreateTime参数类型不正确或缺失')
        parser.add_argument('ModifyTime', type=str, location='args', required=False, help='ModifyTime参数类型不正确或缺失')
        
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        
        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')
        
        res = PassageInfoService.joint_query(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount'], totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def userpublish(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('PostAuthorID', type=str, location='args', required=True, help='PostAuthorID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.userpublish(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalCount=res['totalCount'],
                           totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])


    @classmethod
    def userread_vulnerability_3(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('AutoID', type=int, location='args', required=True, help='AutoID参数类型不正确或缺失')
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.userread_vulnerability_3(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data']
                           )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def userread(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('AutoID', type=int, location='args', required=True, help='AutoID参数类型不正确或缺失')
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.userread(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data']
                           )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def pic(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('file', type=FileStorage, location='files', required=True, help='file参数类型不正确或缺失')
        parser.add_argument('UserID', type=str, location='form', required=False, help='UserID参数类型不正确或缺失')



        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.pic(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'],
                          )
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

        # join table query

    @classmethod
    def getoneall(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('PostAuthorID', type=str, location='args', required=True, help='PostAuthorID参数类型不正确或缺失')

        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.getoneall(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'],person_info=res['person_info'], totalCount=res['totalCount'],
                           totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    #模糊查找
    @classmethod
    def fuzzy_query(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('KeyWord', type=str, location='args', required=True, help='keyword参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('AdminUserID', type=str, location='args',required=False, help='AdminUserID参数类型不正确或缺失')
        parser.add_argument('UserID', type=str, location='args', required=False, help='UserID参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.fuzzy_query(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'],totalCount=res['totalCount'],totalPage=res['totalPage'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    # 推荐
    @classmethod
    def recommend(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.recommend(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['error'])

    # 热门搜索推荐
    @classmethod
    def toprecommend(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('UserID', type=str, location='args', required=True, help='UserID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.toprecommend(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['error'])

    # 按照系查找
    @classmethod
    def deptquery(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('Dept', type=str, location='args', required=True, help='Dept参数类型不正确或缺失')
        parser.add_argument('UserID', type=str, location='args', required=False, help='UserID参数类型不正确或缺失')
        parser.add_argument('Page', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', type=int, location='args', required=False, help='Page参数类型不正确或缺失')
        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.deptquery(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['error'])

    #删除发布文章以及评论
    @classmethod
    def deletepassage(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('PassageID', type=int, location='form', required=True, help='PassageID参数类型不正确或缺失')
        parser.add_argument('UserID', type=str, location='form', required=True, help='UserID参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoService.deletepassage(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def updatev1(cls):
        kwargs = request.form.to_dict()
        # print(request.form)
        # parser = reqparse.RequestParser()
        # # print(parser,'qwerty')
        # parser.add_argument('AutoID', type=int, location='form', required=True, help='AutoID参数类型不正确或缺失')
        # parser.add_argument('PostAuthorID', type=str, location='form', required=False, help='PostAuthorID参数类型不正确或缺失')
        # parser.add_argument('PostContent', type=str, location='form', required=False, help='PostContent参数类型不正确或缺失')
        # parser.add_argument('PostTitle', type=str, location='form', required=False, help='PostTitle参数类型不正确或缺失')
        # parser.add_argument('PostStatus', type=int, location='form', required=False, help='PostStatus参数类型不正确或缺失')
        # parser.add_argument('PostType', type=str, location='form', required=False, help='PostType参数类型不正确或缺失')
        # parser.add_argument('DeptType', type=str, location='form', required=False, help='DeptType参数类型不正确或缺失')
        # parser.add_argument('UpVoteNum', type=int, location='form', required=False, help='UpVoteNum参数类型不正确或缺失')
        # parser.add_argument('CommentNum', type=int, location='form', required=False, help='CommentNum参数类型不正确或缺失')
        # parser.add_argument('LookNUM', type=int, location='form', required=False, help='LookNUM参数类型不正确或缺失')
        # parser.add_argument('HeadlineEntities', type=str, location='form', required=False,
        #                     help='HeadlineEntities参数类型不正确或缺失')
        # parser.add_argument('BodyEntities', type=str, location='form', required=False, help='BodyEntities参数类型不正确或缺失')
        # parser.add_argument('IsDelete', type=int, location='form', required=False, help='IsDelete参数类型不正确或缺失')
        # parser.add_argument('CreateTime', type=str, location='form', required=False, help='CreateTime参数类型不正确或缺失')
        # parser.add_argument('ModifyTime', type=str, location='form', required=False, help='ModifyTime参数类型不正确或缺失')
        #
        # try:
        #     kwargs = parser.parse_args()
        #     kwargs = commons.put_remove_none(**kwargs)
        #     print(kwargs)
        # except Exception as e:
        #     loggings.exception(1, e)
        #     return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoController.update(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])

    @classmethod
    def updatev2(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('PostContent', type=str, location='form', required=False, help='PostContent参数类型不正确或缺失')
        parser.add_argument('PostTitle', type=str, location='form', required=False, help='PostTitle参数类型不正确或缺失')
        parser.add_argument('PostStatus', type=int, location='form', required=False, help='PostStatus参数类型不正确或缺失')
        parser.add_argument('PostType', type=str, location='form', required=False, help='PostType参数类型不正确或缺失')
        parser.add_argument('DeptType', type=str, location='form', required=False, help='DeptType参数类型不正确或缺失')
        parser.add_argument('IsDelete', type=int, location='form', required=False, help='IsDelete参数类型不正确或缺失')
        parser.add_argument('ModifyTime', type=str, location='form', required=False, help='ModifyTime参数类型不正确或缺失')

        try:
            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)
        except Exception as e:
            loggings.exception(1, e)
            return jsonify(code=RET.PARAMERR, message='参数类型不正确或缺失', error='参数类型不正确或缺失')

        res = PassageInfoController.update(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'])
        else:
            return jsonify(code=res['code'], message=res['message'], error=res['error'])
